<?php

namespace App\Console\Commands;

use App\Jobs\TempPush;
use App\Models\Appoint;
use App\Models\ArticleType;
use App\Models\Family;
use App\Models\FamilyEdu;
use App\Models\FamilyType;
use App\Models\Hospital;
use App\Models\HospitalAdministrator;
use App\Models\HospitalAdminUserHospital;
use App\Models\old\Article;
use App\Models\old\ArticleInfo;
use App\Models\old\DoctorHospital;
use App\Models\old\DoctorParent;
use App\Models\old\HospitalAdminRoleUsers;
use App\Models\old\HospitalAdminUser;
use App\Models\old\Interview;
use App\Models\old\OldDoctor;
use App\Models\old\OldHospital;
use App\Models\old\OldHospitals;
use App\Models\old\Pregnancy;
use App\Models\old\UserLogin;
use App\Models\TempPushRecord;
use App\Models\TempUserid;
use App\Wechat\MyAccessToken;
use App\Wechat\Temp;
use Illuminate\Console\Command;

class migrationInterview extends Command
{

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'migration:interview';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $userids = TempUserid::query()->pluck('userid')->toArray();
        $interviews = Interview::query()
            ->whereIn('userid',$userids)
            ->get();
        foreach ($interviews as $interview) {
            $row['prenatal_test'] = $interview->prenatal_test;
            $row['pt_hospital'] = $interview->pt_hospital;
            $row['pt_date'] = date('Y-m-d',$interview->pt_date);
            $row['prenatal'] = $interview->prenatal;
            $row['childbirth_hospital'] = $interview->childbirth_hospital;
            $row['childbirth_date'] = date('Y-m-d',$interview->childbirth_date);
            $row['created_at'] = date('Y-m-d',$interview->createtime);
            $row['childbirth_type'] = $interview->childbirth_type;
            $row['pt_value'] = $interview->pt_value;
            $row['type'] = $interview->week;
            $row['user_id'] = $interview->userid;
            $doctorParent = DoctorParent::query()->where('parentid',$interview->userid)->first();
            $row['hospital_id'] = isset($doctorParent)?$doctorParent->doctorid:0;

            $preg = Pregnancy::query()->where('familyid',$interview->userid)->first();
            if($preg) {
                $family = Family::query()->where('old_id', $preg->id)->where('type',2)->first();
                if ($family) {
                    $familyPreg = Family::query()->where('id', $family->id)->first();
                }
            }
            $row['family_id'] = isset($family)?$family->id:0;
            $row['family_pregn_id'] = isset($familyPreg)?$familyPreg->id:0;
            \App\Models\Interview::query()->updateOrCreate(['user_id'=>$interview->userid,'family_id'=>$row['family_id'],'type'=>$interview->week],$row);
        }

        return 0;
    }
}
